//
//  PickerView.m
//  DeepBreathing
//
//  Created by rimi on 15/12/29.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "PickerView.h"

#import "QuestionnaireManger.h"

@interface PickerView ()

@property(strong,nonatomic)UIButton *saveButton;

@property(strong,nonatomic)UIButton *giveupButton;

@property(strong,nonatomic)UIButton *changeDateButton;

@property(strong,nonatomic)UIButton *changeTimeButton;

@property(strong,nonatomic)UIView *assessMentTimeView;

@property(strong,nonatomic)UILabel *dateLabel;

@property(strong,nonatomic)UILabel *timeLabel;

@property(strong,nonatomic)UIView *datePikerView;

@property(strong,nonatomic)UIDatePicker *datePiker;

@property(strong,nonatomic)UIDatePicker *timePiker;

@property(assign,nonatomic,getter=isChangeDate)BOOL changeDate;

@end

@implementation PickerView

#pragma mark -- init
-(instancetype)init{
    
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        [self createUserInterFace];
    }
    
    
    return self;
  }

#pragma mark -- private methods
-(void)createUserInterFace{
    
    [self addSubview:self.saveButton];
    [self addSubview:self.giveupButton];
    [self addSubview:self.assessMentTimeView];
    [UIView animateWithDuration:0.8 animations:^{
        self.saveButton.center = CGPointMake(375.f/2, 306);
        self.giveupButton.center = CGPointMake(375.f/2, 424);
        self.assessMentTimeView.frame = CGRectMake(0, 527, 375, 140);
    }];
    
    
}

#pragma mark -- BUTTON

-(UIButton *)createButtonWithTitle:(NSString *)title{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.9286 green:1.0 blue:0.8896 alpha:1.0] forState:UIControlStateNormal];
    button.titleLabel.font  = [UIFont systemFontOfSize:16];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor colorWithRed:0.9286 green:1.0 blue:0.8896 alpha:1.0].CGColor;
    button.layer.cornerRadius = 2;
    [button addTarget:self action:@selector(prentPikerViewchooseTime:) forControlEvents:UIControlEventTouchUpInside];
    
    return  button;
    
    
}
-(UIButton *)createButtonInDarePikerViewWithTitle:(NSString *)title{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.layer.cornerRadius = 4;
    [button addTarget:self action:@selector(buttonInDatePikerView:) forControlEvents:UIControlEventTouchUpInside];
    return button;
    
    
}
#pragma mark -- interFace methods
-(void)getRecordTimeWithBlock:(void (^)(NSString *))block{
    
    NSString *assessmentTimeString = [NSString stringWithFormat:@"%@ %@",self.dateLabel.text,self.timeLabel.text];
    block(assessmentTimeString);
    }

-(NSString *)getTimeWithDate:(NSDate *)date isDate:(BOOL)flag{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT+8"];
    if (flag) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        
    }else{
        [dateFormatter setDateFormat:@"HH:mm"];
    }
    
    NSString *currentTime = [dateFormatter stringFromDate:date];
    
    return currentTime;
    
}
#pragma mark -- block
-(void)prentPikerViewchooseTime:(UIButton *)sender{
    
    if (sender.tag == 289) {
        self.changeDate = YES;
        [self.datePikerView addSubview:self.datePiker];
        [self addSubview:self.datePikerView];
        [UIView animateWithDuration:0.5 animations:^{
            self.datePikerView.alpha = 0.9;
        }];
    }else if (sender.tag == 290){
        
        self.changeDate = NO;
        [self.datePikerView addSubview:self.timePiker];
        [self addSubview:self.datePikerView];
        [UIView animateWithDuration:0.5 animations:^{
            self.datePikerView.alpha = 0.9;
        }];
        
        
    }
    
    
    
}
-(void)buttonInDatePikerView:(UIButton *)sender{
    
    if (sender.tag == 292) {
        if (self.isChangeDate) {
            self.dateLabel.text = [self getTimeWithDate:self.datePiker.date isDate:YES];
            [self.datePiker removeFromSuperview];
        }else{
            self.timeLabel.text = [self getTimeWithDate:self.timePiker.date isDate:NO];
            [self.timePiker removeFromSuperview];
        }
        [self.datePikerView removeFromSuperview];
        self.datePikerView.alpha = 0;
    }else if (sender.tag == 291){
        if (self.isChangeDate) {
            [self.datePiker removeFromSuperview];
        }else{
            [self.timePiker removeFromSuperview];
        }
        [self.datePikerView removeFromSuperview];
        self.datePikerView.alpha = 0;
        
        
        
        
    }
    
    
        

}

#pragma mark -- getter
-(UIButton *)saveButton{
    
    if (!_saveButton) {
        _saveButton = ({
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:@"保存"] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"保存2"] forState:UIControlStateHighlighted];
            button.bounds = CGRectMake(0, 0, 100, 100);
            button.center = CGPointMake(375.f/2, 486);
            
            
            button;
        
                });
            }
    
    return _saveButton;

}
-(UIButton *)giveupButton{
    
    if (!_giveupButton) {
        _giveupButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:@"放弃保存" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            button.titleLabel.font = [UIFont systemFontOfSize:17];
           
            button.bounds = CGRectMake(0, 0, 76, 16);
            button.center = CGPointMake(375.f/2, 604);
            
            button;

                  });
    }
    return _giveupButton;
}
-(UIView *)assessMentTimeView{
    
    if (!_assessMentTimeView) {
        _assessMentTimeView = [[UIView alloc]initWithFrame:CGRectMake(0, 717, 375, 140)];
        _assessMentTimeView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];
        UIGraphicsBeginImageContext(CGSizeMake(375, 140));
        
        [@"当前评估日期" drawInRect:CGRectMake(14,19,105,19)    withAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:17]}];
        [@"当前评估时间" drawInRect:CGRectMake(14,81,105,19)    withAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:17]}];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        _assessMentTimeView.layer.contents = (__bridge id)image.CGImage;
        [_assessMentTimeView addSubview:self.dateLabel];
         [_assessMentTimeView addSubview:self.timeLabel];
         [_assessMentTimeView addSubview:self.changeDateButton];
         [_assessMentTimeView addSubview:self.changeTimeButton];

    }
    
    return _assessMentTimeView;
    
    }

- (UIButton *)changeDateButton {
    if (!_changeDateButton) {
        _changeDateButton = [self createButtonWithTitle:@"更改日期"];
        _changeDateButton.tag = 289;
        _changeDateButton.frame = CGRectMake(260, 15, 105, 46);
    }
    return _changeDateButton;
}
- (UIButton *)changeTimeButton {
    if (!_changeTimeButton) {
        _changeTimeButton = [self createButtonWithTitle:@"更改时间"];
        _changeTimeButton.tag = 290;
        _changeTimeButton.frame = CGRectMake(260, 80, 105, 46);
    }
    return _changeTimeButton;
}
- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 42, 115, 20)];
        _dateLabel.textColor = [UIColor whiteColor];
        _dateLabel.font = [UIFont systemFontOfSize:18];
        _dateLabel.text = [self getTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:0] isDate:YES];
    }
    return _dateLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 106, 70, 20)];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.font = [UIFont systemFontOfSize:18];
        _timeLabel.text = [self getTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:0] isDate:NO];
    }
    return _timeLabel;
}
-(UIView *)datePikerView{
    
    if (!_datePikerView) {
        _datePikerView = [[UIView alloc]init];
        _datePikerView.bounds = CGRectMake(0, 0, 258, 432);
        _datePikerView.center = self.center;
        _datePikerView.backgroundColor = [UIColor whiteColor];
        _datePikerView.alpha = 0;
        _datePikerView.layer.cornerRadius = 10;
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 258, 80)];
        view.backgroundColor = [UIColor colorWithRed:1.0 green:0.5969 blue:0.6182 alpha:1.0];
        
        [_datePikerView addSubview:view];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(87, 15, 90, 22)];
        
        label.text = @"更改日期";
        
        label.tintColor = [UIColor whiteColor];
        
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
        
        [_datePikerView addSubview:label];
        
        
        UIGraphicsBeginImageContext(_datePikerView.bounds.size);
        
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        
        CGContextSetStrokeColorWithColor(ctx, [UIColor orangeColor].CGColor);
        
        CGContextSetLineWidth(ctx, 1);
     
        CGPoint point2[] = {CGPointMake(0,360), CGPointMake(258, 360)};
        CGContextStrokeLineSegments(ctx, point2, 2);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        _datePikerView.layer.contents = (__bridge id)image.CGImage;
        UIButton *cancelButton = [self createButtonInDarePikerViewWithTitle:@"取消"];
        cancelButton.tag = 291;
        cancelButton.frame = CGRectMake(12, 380, 90, 40);
        [cancelButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        cancelButton.layer.borderWidth = 1;
        cancelButton.layer.backgroundColor =[UIColor colorWithRed:1.0 green:0.5969 blue:0.6182 alpha:1.0].CGColor;
        [_datePikerView addSubview:cancelButton];
        
        UIButton *sureButton = [self createButtonInDarePikerViewWithTitle:@"确定"];
        sureButton.tag = 292;
        sureButton.frame = CGRectMake(112, 380, 135, 40);
        [sureButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
        sureButton.layer.borderWidth = 1;
        sureButton.layer.backgroundColor =[UIColor colorWithRed:1.0 green:0.5969 blue:0.6182 alpha:1.0].CGColor;
        [_datePikerView addSubview:sureButton];

        
        
    }
    
    return _datePikerView;
    
    
}
-(UIDatePicker *)datePiker{
    
    if (!_datePiker) {
        _datePiker = ({
            
            
            UIDatePicker *datePicker = [[UIDatePicker alloc]init];
            datePicker.datePickerMode = UIDatePickerModeDate;
            datePicker.bounds = CGRectMake(0, 0, 258, 280);
            datePicker.center = CGPointMake(130, 205);
            datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-7*24*60*60];
            datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:0];
            datePicker;
            
            
        });
    }
    return _datePiker;
}
-(UIDatePicker *)timePiker{
    if (!_timePiker) {
        _timePiker = ({
            
            
            
            UIDatePicker *datePicker = [[UIDatePicker alloc]init];
            datePicker.datePickerMode = UIDatePickerModeTime;
            datePicker.bounds = CGRectMake(0, 0, 258, 280);
            datePicker.center = CGPointMake(130, 205);
            datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:0];
            datePicker;
            
        });
        
    }
    return _timePiker;
}


@end
